package com.stock.calculator.model;

import java.io.IOException;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Map;

import com.stock.calculator.helper.StockQuoteHelper;

public class Stock {

    private final StockQuoteHelper stockQuote;

    private final String           tickerSymbol;
    private String                 name;
    private BigDecimal             price;
    private BigDecimal             high;
    private BigDecimal             low;
    private BigDecimal             volume;
    private BigDecimal             marketcap;
    private String                 tradetime;
    private BigDecimal             volumeavg;
    private BigDecimal             pe;
    private BigDecimal             eps;
    private BigDecimal             bookValue;
    private BigDecimal             dividendYield;
    private BigDecimal             high52;
    private BigDecimal             low52;
    private BigDecimal             change;
    private BigDecimal             beta;
    private String                 changepct;
    private BigDecimal             closeyest;
    private BigDecimal             shares;
    private String                 currency;

    public Stock(String tickerSymbol) throws IOException {
        this.tickerSymbol = tickerSymbol;
        stockQuote = new StockQuoteHelper(tickerSymbol);
        refresh();
    }

    public void refresh() throws IOException {
        Map<String, String> quote = stockQuote.fetchQuote();

        name = quote.get("Name");
        price = getValue(quote.get("LastTradePriceOnly"));
        high = getValue(quote.get("DaysHigh"));
        low = getValue(quote.get("DaysLow"));
        volume = getValue(quote.get("Volume"));
        marketcap = getValue(quote.get("MarketCapitalization"));
        tradetime = quote.get("LastTradeTime");
        volumeavg = getValue(quote.get("AverageDailyVolume"));
        pe = getValue(quote.get("PERatio"));
        eps = getValue(quote.get("EarningsShare"));
        bookValue = getValue(quote.get("BookValue"));
        dividendYield = getValue(quote.get("DividendYield"));
        high52 = getValue(quote.get("YearHigh"));
        low52 = getValue(quote.get("YearLow"));
        change = getValue(quote.get("Change"));
        beta = getValue(quote.get(""));
        changepct = quote.get("ChangeinPercent");
        closeyest = getValue(quote.get("PreviousClose"));
        shares = getValue(quote.get(""));
        currency = "USD";
    }

    private static final BigDecimal BILLION = new BigDecimal("1000000000");

    private static BigDecimal getValue(String value) {
        if (value == null) // TODO : StringUtils.isEmpty();
            return null;

        if (value.contains("B")) {
            value = value.replaceAll("B", "");
            return new BigDecimal(value).multiply(BILLION);
        }
        return new BigDecimal(value);
    }

    /**
     * @return the Ticker Symbol
     */
    public String getTickerSymbol() {
        return tickerSymbol;
    }

    /**
     * @return the name of the company
     */
    public String getName() {
        return name;
    }

    /**
     * @return the price of stock
     */
    public BigDecimal getPrice() {
        return price;
    }

    /**
     * @return the day high
     */
    public BigDecimal getHigh() {
        return high;
    }

    /**
     * @return the day low
     */
    public BigDecimal getLow() {
        return low;
    }

    /**
     * @return the volume of stock traded
     */
    public BigDecimal getVolume() {
        return volume;
    }

    /**
     * @return the marketcap
     */
    public BigDecimal getMarketcap() {
        return marketcap;
    }

    /**
     * @return the tradetime
     */
    public String getTradetime() {
        return tradetime;
    }

    /**
     * @return the volume average
     */
    public BigDecimal getVolumeavg() {
        return volumeavg;
    }

    /**
     * @return the P/E
     */
    public BigDecimal getPe() {
        return pe;
    }

    /**
     * @return the EPS
     */
    public BigDecimal getEps() {
        return eps;
    }

    /**
     * @return the BookValue
     */
    public BigDecimal getBookValue() {
        return bookValue;
    }

    /**
     * @return the dividendYield
     */
    public BigDecimal getDividendYield() {
        return dividendYield;
    }

    /**
     * @return the high52
     */
    public BigDecimal getHigh52() {
        return high52;
    }

    /**
     * @return the low52
     */
    public BigDecimal getLow52() {
        return low52;
    }

    /**
     * @return the change
     */
    public BigDecimal getChange() {
        return change;
    }

    /**
     * @return the beta
     */
    public BigDecimal getBeta() {
        return beta;
    }

    /**
     * @return the changepct
     */
    public String getChangepct() {
        return changepct;
    }

    /**
     * @return the closeyest
     */
    public BigDecimal getCloseyest() {
        return closeyest;
    }

    /**
     * @return the shares
     */
    public BigDecimal getShares() {
        return shares;
    }

    /**
     * @return the currency
     */
    public String getCurrency() {
        return currency;
    }

    @Override
    public String toString() {
        Field[] fields = Stock.class.getDeclaredFields();
        
        System.out.println(fields.length);

        StringBuilder message = new StringBuilder();
        for (int i = 1; i < fields.length; ++i) {
            try {
                message.append(fields[i].getName()).append('=').append(fields[i].get(this)).append('\n');
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return message.toString();
    }

    public static void main(String[] args) throws IOException {
        System.out.println(new Stock("ORCL").toString());
    }
}
